package com.csx.stack;

/**
 * 给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串，判断字符串是否有效。
 * @author 陈胤训
 * <p>
 * create: 2019-04-01 16:40
 **/
public class Solution {

    public boolean isValid(String s) {

        ArrayStack<Character> stack = new ArrayStack<>();
        for (int i = 0; i < s.length() ; i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else{
                if (stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if (c == ')' && pop != '('){
                    return false;
                }
                if (c == ']' && pop != '['){
                    return false;
                }
                if (c == '}' && pop != '{'){
                    return false;
                }

            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.isValid("{{{()})}"));
        System.out.println(solution.isValid("(){}[]"));
    }
}
